﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using STF.OrangeDog.IService;
using STF.OrangeDog.IRepository;
using STF.OrangeDog.Dtos;
using STF.OrangeDog.Model;

namespace STF.OrangeDog.Service
{
    public class ClassificationColumn:IClassificationColumnBll
    {
        private IClassifcationColumnDal _dal;

        public ClassificationColumn(IClassifcationColumnDal dal)
        {
            _dal = dal;
        }

       

        public async Task<int> AddClassificationAsync(string title, int uid)
        {
            return await _dal.AddModelAsync(new Model.ClassificationColumn
            {
                title = title,
                uid = uid
            });
        }

        public async Task<int> DeleteClassificationAsync(int Id)
        {
            var data = await _dal.GetDataByIdAsync(Id);
            if (data == null)
                return -1;
            if (data.DeleteId == 1)
                return 0;
            return await _dal.RemoveModelAsync(data);
        }

        public async Task<int> EditClassificationAsync(int Id, string Title, int Uid)
        {
            var data = await _dal.GetDataByIdAsync(Id);
            if (data == null)
                return -1;
            data.title = Title;
            data.uid = Uid;
            data.UpdateTime = DateTime.Now;
            return await _dal.EditModelAsync(data);
        }

        public async Task<IEnumerable<ClassificationColumnDtos>> GetAll(int DeleteId = 1)
        {
            var data = await _dal.GetDataByChooseAsync(r => r.DeleteId == DeleteId);
            return data != null ? data.Select(r => new ClassificationColumnDtos
            {
                id = r.Id,
                title = r.title,
                uid = r.uid,
                
            }) : new List<ClassificationColumnDtos>();
        }

        public async Task<ClassificationColumnDtos> GetDataById(int Id)
        {
            var data = await _dal.GetDataByIdAsync(Id);
            return data == null ? new ClassificationColumnDtos() : new ClassificationColumnDtos
            {
                id = data.Id,
                title = data.title,
                uid = data.uid,
               
            };
        }

        public async Task<IEnumerable<ClassificationColumnDtos>> GetDataByTitle(string Title, int DeleteId = 1)
        {
            var data = await _dal.GetDataByChooseAsync(r => r.DeleteId == DeleteId && r.title.Contains(Title));
            return data == null ? new List<ClassificationColumnDtos>()
                : data.Select(r => new ClassificationColumnDtos
                {
                    id = r.Id,
                    title = r.title,
                    uid = r.uid,
                   
                });
        }
    }
}
